package lv.imho.client.view;

import java.util.ArrayList;
import java.util.List;

import lv.imho.client.model.DataTransferObject;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Represents text list UI component.
 * 
 * @author Igors Gulbinskis
 */
public class TextListPanel extends VerticalPanel {
	
	private final String WIDTH     = "100%";
	private final String HEIGHT    = "100%";	
	private final String AW_WIDTH  = "700px";
	
	private List<DataTransferObject> data;
	private List<TextComponent>      textComponents;
	
	public TextListPanel(List<DataTransferObject> data) {
		super();
		
		this.data = data;		
		this.textComponents = new ArrayList<TextComponent>();		
		setWidth(WIDTH);
		
		initChildComponents();
	}
	
	private void initChildComponents() {
		Label title = new Label("Analizētie teksti <" + data.size() + ">");		
		title.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);		
		DOM.setStyleAttribute(title.getElement(), "background", "#8cabdf");
		DOM.setStyleAttribute(title.getElement(), "fontWeight", "bold");
		add(title);
		
		VerticalPanel wrapper = new VerticalPanel();
		wrapper.setWidth(WIDTH);
		wrapper.setSpacing(5);
		TextComponent textComponent;
		if (data != null && data.size() > 0) {
			for (DataTransferObject text : data) {
				textComponent = new TextComponent(text);
				textComponents.add(textComponent);
				wrapper.add(textComponent);
			}
		}
		
		ScrollPanel anotherWrapper = new ScrollPanel();
		anotherWrapper.setWidth(AW_WIDTH);
		anotherWrapper.setHeight(HEIGHT);
		/* so that only vertical scrollbar is shown */
		// anotherWrapper.setHeight((RootPanel.get("textListContainer").getElement().getClientHeight() - title.getElement().getClientHeight() - (Utils.isMSIE() ? 20 : 25)) + "px");
		// DOM.setStyleAttribute(getElement(), "overflowY", "scroll");
		// DOM.setStyleAttribute(getElement(), "overflowX", "hidden");		
		anotherWrapper.add(wrapper);
		
		add(anotherWrapper);
	}
	
}
